<<<<<<< HEAD
package com.wyz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyz.common.AutoLog;
import com.wyz.common.ResultVo;
import com.wyz.dto.AddStudentsToClassDTO;
import com.wyz.entity.StudentEntity;
import com.wyz.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/12/22 17:57
 */
@RestController
@Slf4j
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    //查询所有学生信息分页显示在页面上
    @AutoLog("进入学生信息管理页面")
    @GetMapping("/queryStudentByPage/{page}")
    public ResultVo<Map<String,Object>> queryStudentByPage(@PathVariable("page") int page) {
        log.info("分页查询学生信息请求,参数:{}", page);
        IPage<StudentEntity> studentListPage = studentService.getStudentListByPage(page);
        List<StudentEntity> studentList = studentListPage.getRecords();//获取分页查询结果
        int total = (int)studentListPage.getPages();//获取总页数
        //构造分页页码列表
        List<Integer> pageList = new ArrayList<>();
        for (int i = 1; i <= total ; i++) {
            pageList.add(i);
        }
        log.info("pageList:{}",pageList);

        //使用Map封装响应的多个数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("studentList",studentList);
        resultMap.put("pageList",pageList);

        log.info("获取分页图书列表响应", resultMap);

        return new ResultVo<>(200,"ok",resultMap);
    }
    //添加学生信息
    @AutoLog("添加学生信息")
    @PostMapping("/addStudent")
    public ResultVo<String> addStudent(@RequestBody StudentEntity studentEntity) {
        log.info("添加学生信息请求,参数:{}", studentEntity);
        try {
            if (studentService.addStudent(studentEntity)){
                log.info("添加学生信息成功:{}",studentEntity);
                return ResultVo.success();
            }
            return ResultVo.fail();
        } catch (Exception e) {
            return ResultVo.fail();
        }
    }
    //通过id查询一条信息
    @AutoLog("通过id查询学生信息")
    @GetMapping("/queryStudentById/{studentId}")
    public ResultVo<StudentEntity> queryStudentById(@PathVariable("studentId") Integer studentId) {
        log.info("通过id查询学生信息请求,参数:{}", studentId);
        StudentEntity studentEntity = studentService.queryStudentById(studentId);
        log.info("通过id查询学生信息响应,参数:{}", studentEntity);
        return new ResultVo<>(200,"ok",studentEntity);
    }
    //修改学生信息
    @AutoLog("修改学生信息")
    @PostMapping("/updateStudent")
    public ResultVo<Boolean> updateStudent(@RequestBody StudentEntity studentEntity) {
        log.info("修改学生信息请求,参数:{}", studentEntity);
        try {
            boolean flag=studentService.updateStudent(studentEntity);
            return new ResultVo<>(200,"ok",flag);
        } catch (Exception e) {
            return ResultVo.fail();
        }
    }
    //删除学生信息
    @AutoLog("删除学生信息")
    @GetMapping("/deleteStudent/{studentId}")
    public ResultVo<Boolean> deleteStudent(@PathVariable("studentId") Integer studentId) {
        log.info("删除学生信息请求,参数:{}", studentId);
        try{
            boolean flag=studentService.deleteStudent(studentId);
            return new ResultVo<>(200,"ok",flag);
        }catch (Exception e){
            return ResultVo.fail();
        }

    }
    //模糊查询：通过姓名或学号模糊查询
    @AutoLog("模糊查询学生信息")
    @GetMapping("/queryStudentByNameOrId/{keyword}")
    public ResultVo<List<StudentEntity>> queryStudentByNameOrId(@PathVariable("keyword") String keyword) {
        log.info("模糊查询请求,参数:{}", keyword);
        List<StudentEntity> studentList = studentService.queryStudentByNameOrId(keyword);
        log.info("模糊查询响应,参数:{}", studentList);
        return new ResultVo<>(200,"ok",studentList);
    }
    //批量将学生加入到班级中,由于学生属性中有班级，故不需要单独添加，只需要修改学生表中的班级class_name即可
    @PostMapping("/addStudentToClass")
    @AutoLog("分班")
    public ResultVo<String> addStudentToClass(@RequestBody AddStudentsToClassDTO addStudentsToClassDTO) {
        List<Integer> studentsId = addStudentsToClassDTO.getStudentsId();
        String studentClass = addStudentsToClassDTO.getStudentClass();
        log.info("批量将学生加入到班级中请求,参数:{}", studentsId, studentClass);
        boolean flag = studentService.addStudentToClass(studentsId, studentClass);
        log.info("批量将学生加入到班级中响应,参数:{}", flag);
        return ResultVo.success();
    }
}
=======
package com.wyz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyz.common.AutoLog;
import com.wyz.common.ResultVo;
import com.wyz.dto.AddStudentsToClassDTO;
import com.wyz.entity.StudentEntity;
import com.wyz.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/12/22 17:57
 */
@RestController
@Slf4j
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    //查询所有学生信息分页显示在页面上
    @AutoLog("进入学生信息管理页面")
    @GetMapping("/queryStudentByPage/{page}")
    public ResultVo<Map<String,Object>> queryStudentByPage(@PathVariable("page") int page) {
        log.info("分页查询学生信息请求,参数:{}", page);
        IPage<StudentEntity> studentListPage = studentService.getStudentListByPage(page);
        List<StudentEntity> studentList = studentListPage.getRecords();//获取分页查询结果
        int total = (int)studentListPage.getPages();//获取总页数
        //构造分页页码列表
        List<Integer> pageList = new ArrayList<>();
        for (int i = 1; i <= total ; i++) {
            pageList.add(i);
        }
        log.info("pageList:{}",pageList);

        //使用Map封装响应的多个数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("studentList",studentList);
        resultMap.put("pageList",pageList);

        log.info("获取分页图书列表响应", resultMap);

        return new ResultVo<>(200,"ok",resultMap);
    }
    //添加学生信息
    @AutoLog("添加学生信息")
    @PostMapping("/addStudent")
    public ResultVo<String> addStudent(@RequestBody StudentEntity studentEntity) {
        log.info("添加学生信息请求,参数:{}", studentEntity);
        try {
            if (studentService.addStudent(studentEntity)){
                log.info("添加学生信息成功:{}",studentEntity);
                return ResultVo.success();
            }
            return ResultVo.fail();
        } catch (Exception e) {
            return ResultVo.fail();
        }
    }
    //通过id查询一条信息
    @AutoLog("通过id查询学生信息")
    @GetMapping("/queryStudentById/{studentId}")
    public ResultVo<StudentEntity> queryStudentById(@PathVariable("studentId") Integer studentId) {
        log.info("通过id查询学生信息请求,参数:{}", studentId);
        StudentEntity studentEntity = studentService.queryStudentById(studentId);
        log.info("通过id查询学生信息响应,参数:{}", studentEntity);
        return new ResultVo<>(200,"ok",studentEntity);
    }
    //修改学生信息
    @AutoLog("修改学生信息")
    @PostMapping("/updateStudent")
    public ResultVo<Boolean> updateStudent(@RequestBody StudentEntity studentEntity) {
        log.info("修改学生信息请求,参数:{}", studentEntity);
        try {
            boolean flag=studentService.updateStudent(studentEntity);
            return new ResultVo<>(200,"ok",flag);
        } catch (Exception e) {
            return ResultVo.fail();
        }
    }
    //删除学生信息
    @AutoLog("删除学生信息")
    @GetMapping("/deleteStudent/{studentId}")
    public ResultVo<Boolean> deleteStudent(@PathVariable("studentId") Integer studentId) {
        log.info("删除学生信息请求,参数:{}", studentId);
        try{
            boolean flag=studentService.deleteStudent(studentId);
            return new ResultVo<>(200,"ok",flag);
        }catch (Exception e){
            return ResultVo.fail();
        }

    }
    //模糊查询：通过姓名或学号模糊查询
    @AutoLog("模糊查询学生信息")
    @GetMapping("/queryStudentByNameOrId/{keyword}")
    public ResultVo<List<StudentEntity>> queryStudentByNameOrId(@PathVariable("keyword") String keyword) {
        log.info("模糊查询请求,参数:{}", keyword);
        List<StudentEntity> studentList = studentService.queryStudentByNameOrId(keyword);
        log.info("模糊查询响应,参数:{}", studentList);
        return new ResultVo<>(200,"ok",studentList);
    }
    //批量将学生加入到班级中,由于学生属性中有班级，故不需要单独添加，只需要修改学生表中的班级class_name即可
    @PostMapping("/addStudentToClass")
    @AutoLog("分班")
    public ResultVo<String> addStudentToClass(@RequestBody AddStudentsToClassDTO addStudentsToClassDTO) {
        List<Integer> studentsId = addStudentsToClassDTO.getStudentsId();
        String studentClass = addStudentsToClassDTO.getStudentClass();
        log.info("批量将学生加入到班级中请求,参数:{}", studentsId, studentClass);
        boolean flag = studentService.addStudentToClass(studentsId, studentClass);
        log.info("批量将学生加入到班级中响应,参数:{}", flag);
        return ResultVo.success();
    }
}
>>>>>>> be516e15a35375b38cf973b1ce88c92dd1ceba03
